package com.tedu.rechargeOrder.service.impl;

import com.tedu.rechargeOrder.mapper.RechargeOrderMapper;
import com.tedu.rechargeOrder.pojo.dto.RechargeOrderQuery;
import com.tedu.rechargeOrder.pojo.dto.RechargeOrderSavaParam;
import com.tedu.rechargeOrder.pojo.entity.RechargeOrder;
import com.tedu.rechargeOrder.pojo.vo.RechargeOrderVO;
import com.tedu.rechargeOrder.service.RechargeOrderService;
import com.tedu.user.mapper.UserMapper;
import com.tedu.user.pojo.dto.UserQuery;
import com.tedu.user.pojo.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RechargeOrderServiceImpl implements RechargeOrderService {
    @Autowired
    RechargeOrderMapper rechargeOrderMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public List<RechargeOrderVO> selectOrder(RechargeOrderQuery query) {
        log.debug("查询充值订单业务:select{}",query);
        //查询订单
        List<RechargeOrderVO> list = rechargeOrderMapper.select(query);
        log.debug("入参{}",query);
        log.debug("出参{}",list);
        return list;
    }

    @Override
    public void create(RechargeOrderSavaParam savaParam) {
        log.debug("创建充值订单业务:create{}", savaParam);
        log.debug(savaParam.getStatus().toString());
        //
        UserQuery userQuery = new UserQuery();
        userQuery.setUsername(savaParam.getUsername());
        List<UserVO> userVOList = userMapper.selectUser(userQuery);
        Long UserId = userVOList.get(0).getUserId();
        //
        RechargeOrder rechargeOrder = new RechargeOrder();
        BeanUtils.copyProperties(savaParam,rechargeOrder);
        rechargeOrder.setCreateTime(new Date());
        rechargeOrder.setUserId(UserId);
        rechargeOrderMapper.insert(rechargeOrder);
    }

    @Override
    public void deleteById(Long orderId) {
        log.debug("删除充值订单业务:deleteById{}",orderId);
        rechargeOrderMapper.delete(orderId);
    }

    @Override
    public void update(RechargeOrderSavaParam upparam) {
        log.debug("更新充值订单业务:update{}", upparam);
        RechargeOrder rechargeOrder1 = new RechargeOrder();
        BeanUtils.copyProperties(upparam,rechargeOrder1);
        rechargeOrder1.setUpdateTime(new Date());
        rechargeOrderMapper.update(rechargeOrder1);
    }
}
